package com.prp.gwt.client.widgets.mail;

import com.google.gwt.event.dom.client.ClickEvent;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.IListener;
import com.prp.gwt.client.user.mail.MailListener;
import com.prp.gwt.client.vo.MailboxInfo;
import com.prp.gwt.client.widgets.ConfirmDialog;
import com.prp.gwt.client.widgets.IWidget;

public class InMailboxItem extends HorizontalPanel {
	private MailboxInfo mailboxInfo = new MailboxInfo();
	
	public InMailboxItem(MailboxInfo mailbox) {
		this.mailboxInfo  = new MailboxInfo();
		mailboxInfo.copy(mailbox);
		init();
	}

	private void init() {
		this.setWidth("100%");
		add(getSenderInfoPanel());
		add(getTitlePanel());
		add(getOperationPanel());
	}

	private Widget getTitlePanel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(15);
		panel.setWidth("400px");
		
		String titleString = mailboxInfo.getTitle();
		if(titleString.length() > 60){
			titleString = titleString.substring(0,57);
			titleString += "...";
		}
	
		
		switch (mailboxInfo.getState()) {
		case ClientConstants.MAIL_STATE_READ_EXIST_ALL:
		case ClientConstants.MAIL_STATE_READ_EXIST_RECEIVE:
		case ClientConstants.MAIL_STATE_READ_EXIST_SEND:
			break;
		case ClientConstants.MAIL_STATE_UNREAD_EXIST_ALL:
		case ClientConstants.MAIL_STATE_UNREAD_EXIST_RECEIVE:
		case ClientConstants.MAIL_STATE_UNREAD_EXIST_SEND:
			//titleString = "<B>"+titleString + "</B>";
			break;
		default:
			break;
		}
		HTML title = new HTML(titleString);
		panel.add(title);
		return panel;
	}

	private HorizontalPanel getSenderInfoPanel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(15);
		panel.setWidth("100px");
		panel.add(new HTML(mailboxInfo.getSenderName().toString()));

		Image portrait = mailboxInfo.getSenderPortrait();
		portrait.setWidth("40px");
		portrait.setHeight("40px");
		panel.add(portrait);
		
		return panel;
	}

	
	private HorizontalPanel getOperationPanel() {
		HorizontalPanel operationPanel = new HorizontalPanel();
		operationPanel.setSpacing(10);

		HTML lookover = new HTML("<a>查看</a>");
		HTML delete = new HTML("<a>删除</a>");

		lookover.setVisible(true);
		lookover.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				MailListener.getInstance().lookoverInbox(mailboxInfo);
			}
		});
		operationPanel.add(lookover);

		delete.setVisible(true);
		delete.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ConfirmDialog.getInstance().setCallFunction(new DeleteWidget(),
						null);
				String title = "删除邮件";
				ConfirmDialog.getInstance().show(title, "确定要删除该邮件吗？<br/><br/>");
			}
		});
		operationPanel.add(delete);

		return operationPanel;
	}

	class DeleteWidget implements IWidget {

		@Override
		public void callFunction(Object arg) {
			MailListener.getInstance().deleteInbox(mailboxInfo);
		}

		@Override
		public void setListener(IListener listener) {

		}

	}
}
